package com.itheima.springbootcondition.condition;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

import java.util.Map;

/**
 * @Description:
 * @Version: V1.0
 */
public class ClassCondition implements Condition {
    /**
     *
     * @param Context:上下文对象;用于获取环境,IOC容器ClassLoader对象
     * @param Metadata:注解元对象;用于获取定义注解的属性值
     * @return
     */
    @Override
    public boolean matches(ConditionContext Context, AnnotatedTypeMetadata Metadata) {
        /*//1.需求:导入redis坐标后,创建bean
        try {
            Class<?> cls = Class.forName("redis.clients.jedis.Jedis");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return true;*/


        //2.需求:导入通过注解属性值value指定坐标后创建Bean

        //获取注解属性值   value
        Map<String, Object> map = Metadata.getAnnotationAttributes(ConditionOnClass.class.getName());
        //System.out.println(map);  //{value=[redis.clients.jedis.Jedis]}
        String[] value = (String[]) map.get("value");
        boolean flag = true;

        try {
            for (String className : value) {
                Class<?> cls = Class.forName(className);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            flag = false;
        }
        return flag;
    }
}
